/*
2019-05-10
手动实现new的过程
手动实现Object.create过程
*/

// 构造函数
function User(name, age, address){
  this.name=name;
  this.age=age;
  this.address=address;
}
User.prototype.toString=function(){
  return this.name;
}

// 模拟new的过程
function New_custom(Construcutor, ...params){
  //debugger;
  if(Object.prototype.toString.call(Construcutor)==='[object Function]'){
    let custom_obj={};
    custom_obj.__proto__=Construcutor.prototype;
    // 将声明了this.xxx的变量赋值非custom_obj
    Construcutor.apply(custom_obj, params);
    return custom_obj;
  }
}

var ccf = New_custom(User, 'chen', 28, '地球');
console.log(ccf.toString());


var ccf_bak=Object.create(ccf,{
  a: {
    value: 1
  },
  // foo会成为所创建对象的数据属性
  foo: { 
    writable:true,
    configurable:true,
    value: "hello" 
  },
  // bar会成为所创建对象的访问器属性
  bar: {
    configurable: false,
    get: function() { return 10 },
    set: function(value) {
      console.log("Setting `o.bar` to", value);
    }
  }
});

console.log(ccf_bak.bar);
ccf_bak.bar=111;
var Objectcreate_custom=function(param_object){
  var F=function(){}
  F.prototype=param_object;
  return new F();
}



String.prototype.toString=function(){
  return Array.prototype.slice.call(this).join('');
}
var s1=new String('我是一个...');
console.log(s1);
console.log(s1.toString())
